﻿package net.cmath.service;


import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;






import net.cmath.auth.AuthUser;
import net.cmath.mapper.r.UserMapperR;
import net.cmath.mapper.w.UserMapperW;
import net.cmath.po.FlexObject;
import net.cmath.po.User;

import org.springframework.flex.remoting.RemotingDestination;
import org.springframework.stereotype.Service;

import flex.messaging.FlexContext;

@Service("helloWorldService")
@RemotingDestination(channels = { "person-amf" })
public class HelloWorldService {
	
	@Resource(name="userMapperR")
	private UserMapperR userMapperR;
	
	@Resource(name="userMapperW")
	private UserMapperW userMapperW;
	
	public void setUserMapperR(UserMapperR userMapperR) {
		this.userMapperR = userMapperR;
	}
	
	public void setUserMapperW(UserMapperW userMapperW) {
		this.userMapperW = userMapperW;
	}

	public FlexObject hello(String var) {
		
		User u = new User();
		u.setId(1);
		User user = userMapperR.getUserById(u);
		if (user == null) {
			return FlexObject.returnObject(405);
		}
		String var1 = user.getUserName()+user.getPassword();
		
		//HashMap作为参数
		HashMap<String,Object> map =new HashMap<String,Object>(3);
		map.put("tableName", "user");
		map.put("userName", "t");
		map.put("start", 0);
		map.put("size", 2);
		List<User> list = userMapperR.search(map);
		for(User us : list) {
			System.out.println(us.getUserName() + "\t" + us.getPassword());
		}
		
		Object name = FlexContext.getFlexSession().getAttribute("name");
		if (name == null) {
			System.out.println("Session is null");
		} else {
			System.out.println("aaa:" + FlexContext.getFlexSession().getAttribute("name"));
		}
		
		FlexContext.getFlexSession().setAttribute("name", var);
		FlexContext.getHttpRequest().getCookies();
		
		return FlexObject.returnObject("hello " + var1);
	}

	public FlexObject world(String var1) {
		User user = new User();
		user.setUserName("U" + var1);
		user.setPassword("P" + var1);
		userMapperW.insertUser(user);
		return FlexObject.returnObject("world " + var1);
	}
	
	public FlexObject test(int var) {
		return FlexObject.returnCode(var);
	}
	
	public FlexObject login() {
		FlexContext.getHttpResponse().addCookie(new Cookie("AI","123456789"));
		AuthUser u = new AuthUser();
		u.setUserId(123456789);
		FlexContext.getFlexSession().setAttribute("authInfo", u);
		return FlexObject.returnObject("登陆成功");
	}
	
	public FlexObject throwE() {
		AuthUser u = null;
		u.getUserId();
		return FlexObject.returnObject(1234);
	}
}
